﻿using LeaderCCSLaserUI.Models;
using LeaderCCSLaserUI.Services;
using MotionRobot.Models;
using OxyPlot.Axes;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace LeaderCCSLaserUI.ViewModels.Dialogs.BarcodeScan
{
    public class AddBarcodePointDialogViewModel : DialogViewModel
    {
        #region 变量
        private readonly IGTSCardService gTSCard;
        #endregion
        #region 属性
        public HandEnum Hand { get; set; }
        private double x;
        public double X
        {
            get { return x; }
            set { SetProperty(ref x, value); }
        }
        private double y;
        public double Y
        {
            get { return y; }
            set { SetProperty(ref y, value); }
        }
        private double z;
        public double Z
        {
            get { return z; }
            set { SetProperty(ref z, value); }
        }
        public BarcodeEnum Kind { get; set; }
        public ProductEnum ProductIndex { get; set; }
        public double ExposureTime { get; set; }
        public bool UseLight1 { get; set; }
        public bool UseLight2 { get; set; }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> operateCommad;
        public DelegateCommand<object> OperateCommad =>
            operateCommad ?? (operateCommad = new DelegateCommand<object>(ExecuteOperateCommad));

        private DelegateCommand getPosCommand;
        public DelegateCommand GetPosCommand =>
       getPosCommand ?? (getPosCommand = new DelegateCommand(ExecuteGetPosCommand));
        void ExecuteGetPosCommand()
        {
            if (gTSCard.Connected)
            {
                X = Math.Round(GTSCard.GetEnc(Hand == HandEnum.左 ? gTSCard.A1 : gTSCard.A5), 3);
                Y = Math.Round(GTSCard.GetEnc(Hand == HandEnum.左 ? gTSCard.A2 : gTSCard.A6), 3);
                Z = Math.Round(GTSCard.GetEnc(Hand == HandEnum.左 ? gTSCard.A3 : gTSCard.A7), 3);
            }
        }
        void ExecuteOperateCommad(object obj)
        {
            switch (obj.ToString())
            {
                case "确认":
                    DialogParameters param = new DialogParameters();
                    param.Add("Hand", Hand);
                    param.Add("Kind", Kind);
                    param.Add("X", X);
                    param.Add("Y", Y);
                    param.Add("Z", Z);
                    param.Add("ProductIndex", ProductIndex);
                    param.Add("ExposureTime", ExposureTime);
                    param.Add("UseLight1", UseLight1);
                    param.Add("UseLight2", UseLight2);
                    OnRequestClose(new DialogResult(ButtonResult.Yes, param));
                    break;
                case "取消":
                    OnRequestClose(new DialogResult(ButtonResult.None));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            Hand = HandEnum.左;
            X = 0;
            Y = 0;
            Z = 0;
            Kind =  BarcodeEnum.产品码;
            ProductIndex = ProductEnum.产品1;
            ExposureTime = 100;
            UseLight1 = true;
            UseLight2 = true;
        }
        #endregion
        #region 构造函数
        public AddBarcodePointDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            gTSCard = containerProvider.Resolve<IGTSCardService>("GTSCard");
        }
        #endregion

    }
}
